<html>
<head>
<title>Install rsTrack</title>
</head>
<body>
<h1>Install rsTrack</h1>
<?php
#big ass installation thingy.

$errors = array(); #initalize errors.
if(isset($_POST['submit'])) {
		foreach($_POST as $var => $value) {
			if($value == '') {
				if(($var != 'prefix') && ($var != 'dbPass')) {
					$errors[] = 'You must enter a value for <b>' . str_replace("Db", "Database ", ucwords($var)) . '</b>.';
					}
				}
			}
			$connect = @mysql_connect($_POST['dbHost'],$_POST['dbUser'],$_POST['dbPass']);
				if($connect == false) {
					$errors[] = 'Could not establish connection to MySql server. Make sure your credientials are correct.';
					}
					else {
						$dbCheck = mysql_select_db($_POST['dbName'], $connect);
						if($dbCheck == false) {
							$errors[] = 'Connected to MySql server, but couldn\'t connect to database. Please make sure it exists and the name of the database cooresponds to the one you entered.';
							}
						}
				if(count($errors) /* still? */ == 0) {
					/*MASSIVE SQLS!!!! */
					#create user table
					$_POST['prefix'] = strtolower($_POST['prefix']);
					mysql_query('CREATE TABLE IF NOT EXISTS `' . $_POST['prefix'] . 'users` (
								  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
								  `track_id` int(10) unsigned DEFAULT NULL,
								  `username` varchar(12) DEFAULT NULL,
								  `password` varchar(15) DEFAULT NULL,
								  `email` varchar(320) DEFAULT NULL,
								  `register_date` datetime DEFAULT NULL,
								  `last_active` datetime DEFAULT NULL,
								  `privileges` tinyint(3) unsigned DEFAULT "0",
								  PRIMARY KEY (`id`)
								) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;');
								
					mysql_query('REPLACE INTO `rst_users` (`track_id`, `username`, `password`, `email`, `register_date`, `last_active`, `privileges`) 
								 VALUES(1, "admin", "admin", "admin", now(), now(), 1);');
					
					
					#create data table
					$array = array("overall", "attack", "defence", "strength", "hitpoints", "ranged", "prayer", "magic", "cooking", "wc", "fletching", "fishing", "firemaking", "crafting", "smithing", "mining", "herblore", "agility", "thieving", "slayer", "farming", "rc", "hunter", "construct", "summoning");
					$array2 = array("rank", "level", "xp");

					$var1_1 = 'CREATE TABLE IF NOT EXISTS `' . $_POST['prefix'] . 'data` ('
					  . '`id` int unsigned NOT NULL AUTO_INCREMENT,'
					  . '`user_id` int(10) unsigned DEFAULT NULL,'
					  .	'`datetime` datetime DEFAULT NULL,';
					  
					 $var1_2 = 'CREATE TABLE IF NOT EXISTS `' . $_POST['prefix'] . 'gains` ('
					  . '`id` int unsigned NOT NULL AUTO_INCREMENT,'
					  . '`user_id` int(10) unsigned DEFAULT NULL,'
					  .	'`datetime` datetime DEFAULT NULL,';
					 
					$var2 = '';
					foreach($array as $value) {
						foreach($array2 as $value2) {
							$var3 = $value . '_' . $value2 . " int(10) unsigned DEFAULT NULL,";
							$var2 = $var2. $var3;
							}
						}

					$var4 = 'PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1';	
						
						
					mysql_query($var1_1 . $var2 . $var4);
					mysql_query($var1_2 . $var3 . $var4);
					
					$installed = true;
					}
					
			}
	


if(count($errors) !== 0) {
	echo "<h1>" . count($errors) . " errors returned.</h1>";
	echo '<ul>';
		foreach($errors as $error) {
			echo '<li>' . $error . '</li>';
			}
	echo '</ul>';
	}
	elseif($installed == true) {
		echo "<h1>Installation Success</h1>";
		echo '<ul>';
			echo '<li>Created `' . $_POST['prefix'] . 'data` table in `' . $_POST['dbName'] . '`.</li>';
			echo '<li>Created `' . $_POST['prefix'] . 'users` table in `' . $_POST['dbName'] . '`.</li>';
		echo '</ul>';
		echo '<h1>CLICK <a href="../lib/del_on_install.php">HERE</a> TO CONTINUE</a></h1>';
		}
?>
<form action="" method="post">
	<table>
		<tr>
			<td>Database Host: </td>
			<td><input type="text" name="dbHost" size="30" /></td>
			<td>The name of your database host.</td>
		</tr>
		<tr>
			<td>Database Username: </td>
			<td><input type="text" name="dbUser" size="30" /></td>
			<td>Your database username.</td>
		</tr>
		<tr>
			<td>Database Password: </td>
			<td><input type="text" name="dbPass" size="30" /></td>
			<td>Your database password.</td>
		</tr>
		<tr>
			<td>Database Name: </td>
			<td><input type="text" name="dbName" size="30" /></td>
			<td>The name of your database
		</tr>
		<tr>
			<td>Title: </td>
			<td><input type="text" name="title" size="30" value="rsTrack" /></td>
			<td>The title appears on the browser window, the window tab, and at the top of the webpage.</td>
		</tr>
		<tr>
			<td>Table Prefix: </td>
			<td><input type="text" name="prefix" size="30" value="rsTrack_" /></td>
			<td>The prefix you'd like to have on your table names. If you are only using the database for tracking skills, you should leave this field blank.</td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="submit" value="Install"></td>
			<td></td>
		</tr>
	</table>
</form>
</body>
</html>


